package com.vancouver.knows.ui.fragment.citydiscount;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.view.compactcalendarview.AnimationListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

/**
 * Created by Chris Li on 2016-08-19.
 */
public class WeChatView {

    @Bind(R.id.we_chat_id)
    TextView mWeChatIdText;
    @Bind(R.id.we_chat_qr)
    ImageView mWeChatQrImage;

    private View mRootView;

    private Context mContext;
    private String mWeChatId;
    private String mWeChatQr;

    public WeChatView(Context context, String weChatId, String weChatQr) {
        this.mContext = context;
        this.mWeChatId = weChatId;
        this.mWeChatQr = weChatQr;
        init();
    }

    private void init() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        mRootView  = layoutInflater.inflate(R.layout.view_we_chat_dialog, null, false);
        ButterKnife.bind(this, mRootView);

        mRootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        mWeChatIdText.setText(mWeChatId);
        Picasso.with(mContext).load(mWeChatQr)
                .fit()
                .into(mWeChatQrImage);
        mRootView.setVisibility(View.GONE);
    }

    @OnClick(R.id.we_chat_id)
    public void onWeChatIdClicked() {
        ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText(mContext.getString(R.string.qr_title), mWeChatId);
        clipboard.setPrimaryClip(clip);
        Toast.makeText(mContext, R.string.copied, Toast.LENGTH_SHORT).show();
    }

    @OnLongClick(R.id.we_chat_qr)
    public boolean onWeChatQrLongClicked() {
        Bitmap qrBitmap = ((BitmapDrawable) mWeChatQrImage.getDrawable()).getBitmap();
        if (qrBitmap != null) {
            MediaStore.Images.Media.insertImage(mContext.getContentResolver(), qrBitmap, mWeChatId , "WeChatQR");
        }
        Toast.makeText(mContext, R.string.saved_prompt, Toast.LENGTH_SHORT).show();
        return true;
    }

    public View getWeChatView() {
        return mRootView;
    }

    public void setVisibility(int visibility) {

        if (visibility == View.VISIBLE) {
            Animation fadeIn = new AlphaAnimation(0, 1);
            fadeIn.setInterpolator(new DecelerateInterpolator());
            fadeIn.setDuration(250);
            mRootView.startAnimation(fadeIn);
            mRootView.setVisibility(visibility);
        } else if (visibility == View.GONE) {
            Animation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setInterpolator(new AccelerateInterpolator());
            fadeOut.setDuration(250);
            mRootView.startAnimation(fadeOut);
            fadeOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    super.onAnimationEnd(animation);
                    mRootView.setVisibility(View.GONE);
                }
            });
        }

    }

}
